<?php


namespace App\GatewayWorker;


use App\Models\TradeProduct;
use App\Services\Base\CacheService;
use App\Services\QuotaServices;
use Faker\Factory;
use GatewayWorker\Lib\Gateway;
use Illuminate\Support\Facades\Log;
use Workerman\Lib\Timer;

define("ERROR_CHAT", 201);

class Events
{
    public static function onWorkerStart($businessWorker)
    {
        echo "BusinessWorker    Start\n";
        Timer::add(1, function () {
            self::sendQuota();
        });
    }

    public static function sendQuota()
    {
        $faker = Factory::create();
        $product = TradeProduct::getAll();
        foreach ($product as $v) {
            $price = QuotaServices::getLastByQuota($v['quota_symbol']);
            if ($price) {
                $data = [];
                $data['symbol'] = $v['symbol'];
                $data['price'] = asNumberFormat($price, $v['decimal']);
                $bids = [];
                $asks = [];
                for ($i = 0; $i < 7; $i++) {//买入盘口
                    $bids[] = [$faker->randomFloat($v['decimal'], $price * (1 + random_int(1, 5) / 10000), $price * (1 + random_int(6, 10) / 10000)), $faker->randomFloat(2, 10, 1000)];
                }
                for ($i = 0; $i < 7; $i++) { //卖出盘口
                    $asks[] = [$faker->randomFloat($v['decimal'], $price * (1 - random_int(6, 10) / 10000), $price * (1 - random_int(1, 5) / 10000)), $faker->randomFloat(2, 10, 1000)];
                }

                $data['asks'] = $asks;
                $data['bids'] = $bids;
                $data['rate_lastday'] = QuotaServices::getUpDown($v['quota_symbol'], $price);
                Gateway::sendToAll(json_encode(["mode" => "quota", 'data' => $data]));
            }
        }

    }

    public static function onConnect($client_id)
    {
        echo "onConnect Start {$client_id}\n";
        Gateway::sendToClient($client_id, json_encode(['type' => 'init', 'client_id' => $client_id]));
    }

    public static function onWebSocketConnect($client_id, $data)
    {
//        echo "onWebSocketConnect Start {$client_id}\n";
//        if (!isset($data['get']['token'])) { //如果没有token关闭
//            Gateway::closeClient($client_id);
//        }
    }

    public static function onMessage($client_id, $message)
    {
        $response = ['code' => 0, 'msg' => 'ok', 'data' => []];
//        echo "onMessage Start {$client_id} {$message}\n";
        $message = json_decode($message);
        if (isset($message->mode)) {
            switch ($message->mode) {
                case "say":
                    break;
                case "heart":
                    return;
            }
        } else {
            return;
        }

        Gateway::sendToClient($client_id, json_encode($response));
    }

    public static function onClose($client_id)
    {
        Log::info('close connection' . $client_id);
    }

    public static function sendMessageFromOut($msg, $uid = 0)
    {
        if ($uid > 0) {
            Gateway::sendToUid($uid, $msg);
        } else {
            Gateway::sendToAll($msg);
        }
    }
}
